package scalatest.org

import org.scalatest.{BeforeAndAfter, FunSpec}

import scala.collection.mutable.Stack

/**
  * Created by shichen on 2017/6/21.
  * http://www.scalatest.org/getting_started_with_fun_spec
  */
class ExampleSepc extends FunSpec{
  describe("A stack from http://www.scalatest.org/getting_started_with_fun_spec") {
    it ("should pop values in last-in-first-out order - pending") (pending)
    it ("should throw NoSuchElementException if an empty stack is popped - pending") (pending)

    it("should pop values in last-in-first-out order") {
      val stack = new Stack[Int]
      stack.push(1)
      stack.push(2)
      assert(stack.pop() === 2) //比==有更详细的错误报告
      assert(stack.pop() === 1)
    }

    it("should throw NoSuchElementException if an empty stack is popped") {
      val emptyStack = new Stack[Int]
      //emptyStack.push(1)
      //如果抛出错误异常或没有异常，test failed
      intercept[NoSuchElementException] {
        emptyStack.pop()
      }
    }
  }
}


class ExampleSpec2 extends FunSpec with BeforeAndAfter {

  var stack: Stack[Int] = _

  before {
    stack = new Stack[Int] //每个测试前都要执行的语句
  }

  describe("A Stack") {

    it("should pop values in last-in-first-out order") {
      stack.push(1)
      stack.push(3)
      assert(stack.pop() === 2)
      assert(stack.pop() === 1)
    }

    it("should throw NoSuchElementException if an empty stack is popped") {
      intercept[NoSuchElementException] {
        stack.pop()
      }
    }
  }
}